/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package dk.redillusions.jdbc.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Invocation handler for use when creating proxy {@link Connection}s.
 * 
 * @author Sune Wettersteen
 * 
 */
final class ConnectionInvocationHandler implements InvocationHandler {
    /**
     * The actual jdbc connection.
     */
    private Connection actualConnection;

    /**
     * The list of connections proxies.
     */
    private List<Connection> connections = new ArrayList<Connection>();

    /**
     * Constructor.
     * 
     * @param connection
     *            The underlying connection
     */
    public ConnectionInvocationHandler(final Connection connection) {
        this.actualConnection = connection;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
        // Run through all the connection implementations and invoke the method
        for (Connection connection : this.connections) {
            /*
             * We do not handle exceptions thrown from this. It is not
             * acceptable that any are thrown
             */
            method.invoke(connection, args);
        }

        // Invoke the method on the actual connection
        try {
            return method.invoke(this.actualConnection, args);
        } catch (InvocationTargetException e) {
            final Throwable cause = e.getCause();

            // The exceptions we accept to be thrown
            if (cause instanceof SQLException) {
                throw cause;
            }

            throw e;
        }
    }

    /**
     * Add a connection delegate to the invocation handler. All methods invoked
     * on any proxy for this invocation handler will be delegated to the
     * connection delegators.
     * 
     * @param connection
     *            The connection delegate to add
     */
    public void addConnection(final Connection connection) {
        this.connections.add(connection);
    }

}
